home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
090
/
cmln0385.arc
/
MODULA1.LTG
< prev
next >
Wrap
Text File
|
1986-02-27
|
7KB
|
245 lines
Module2Listing 1. The implementation module of MatMan.
IMPLEMENTATION MODULE MatMan;
(*-------------------------------------------------------------------*)
(* Modula-2 Matrix Management Module *)
(* *)
(* Copyright (c) 1984 by Namir Clement Shammas *)
(* Version 1.0, August 31, 1984 *)
(*-------------------------------------------------------------------*)
PROCEDURE Loc( Row, Col : CARDINAL): CARDINAL;
(* Procedure to locate the (i,j) matrix element, with a default *)
(* HIROW number of rows. *)
BEGIN
RETURN ( (Col - 1) * HIROW + Row)
END Loc;
(*-------------------------------------------------------------------*)
PROCEDURE Loc0( Row, Col : CARDINAL): CARDINAL;
(* Procedure to locate the (i,j) matrix element, with a default *)
(* HIROW number of rows. Used in subroutines with open arrays. *)
BEGIN
RETURN ( (Col - 1) * HIROW + Row - 1)
END Loc0;
(*-------------------------------------------------------------------*)
PROCEDURE LOC( Row, Col, HiRow : CARDINAL): CARDINAL;
(* Procedure to locate the (i,j) matrix element, with an assigned *)
(* HiRow number of rows. *)
BEGIN
RETURN ( (Col - 1) * HiRow + Row)
END LOC;
(*-------------------------------------------------------------------*)
PROCEDURE LOC0( Row, Col, HiRow : CARDINAL): CARDINAL;
(* Procedure to locate the (i,j) matrix element, with an assigned *)
(* HiRow number of rows. Used in subroutines with open arrays. *)
BEGIN
RETURN ( (Col - 1) * HiRow + Row - 1)
END LOC0;
(*-------------------------------------------------------------------*)
PROCEDURE Swap( VAR A, B : REAL);è(* Procedure to swap two REALs *)
VAR Tempo : REAL;
BEGIN
Tempo := A;
A := B;
B := Tempo;
END Swap;
(*-------------------------------------------------------------------*)
PROCEDURE SwapColumn(VAR X : ARRAY OF REAL; Col1, Col2, HiRow : CARDINAL);
(* Procedure to swap two columns, Col1 and Col2 *)
VAR i, L1, L2 : CARDINAL;
BEGIN
L1 := LOC0(1,Col1,HiRow);
L2 := LOC0(1,Col2,HiRow);
FOR i := 0 TO HiRow-1 DO
Swap(X[L1+i],X[L2+i])
END;
END SwapColumn;
(*-------------------------------------------------------------------*)
PROCEDURE SwapRow(VAR X : ARRAY OF REAL; Row1, Row2, HiRow, HiCol : CARDINAL);
(* Procedure to swap two rows, Row1 and Row2 *)
VAR i, R1, R2 : CARDINAL;
BEGIN
R1 := LOC0(Row1,1,HiRow);
R2 := LOC0(Row2,1,HiRow);
FOR i := 1 TO HiCol DO
Swap(X[R1],X[R2]);
INC(R1,HiRow);
INC(R2,HiRow)
END;
END SwapRow;
(*-------------------------------------------------------------------*)
PROCEDURE Transpose(VAR X, Y : ARRAY OF REAL; HiRow, HiCol : CARDINAL);
(* Procedure to transpose matrix X into matrix Y *)
VAR i, j : CARDINAL;
BEGIN
FOR i := 1 TO HiRow DO
FOR j := 1 TO HiCol DO
Y[LOC0(j,i,HiCol)] := X[LOC0(i,j,HiRow)]è END; (* FOR *)
END; (* FOR *)
END Transpose;
(*-------------------------------------------------------------------*)
PROCEDURE InsertColumn(VAR X, Y : ARRAY OF REAL; NewCol : CARDINAL;
VAR HiRow, HiCol : CARDINAL);
(* Procedure to insert a new array Y at column NewCol in matrix X *)
VAR i, L, MatSize : CARDINAL;
BEGIN
IF NewCol <= HiCol THEN
L := LOC0(1,NewCol,HiRow);
MatSize := HiRow * HiCol; (* Initial matrix size *)
(* Move element upward in matrix X *)
FOR i := MatSize - 1 TO L BY -1 DO
X[i + HiRow] := X[i]
END;
(* Insert column Y *)
FOR i := 0 TO HiRow - 1 DO
X[L+i] := Y[i]
END;
INC(HiCol);
END; (* IF *)
END InsertColumn;
(*-------------------------------------------------------------------*)
PROCEDURE InsertRow(VAR X, Y : ARRAY OF REAL; NewRow : CARDINAL;
VAR HiRow, HiCol : CARDINAL);
(* Procedure to insert a new array Y at row NewRow in matrix X *)
VAR i, j, L1, L2 : CARDINAL;
BEGIN
IF NewRow <= HiRow THEN
FOR i := HiCol TO 1 BY -1 DO
L1 := LOC0(HiRow,i,HiRow);
L2 := LOC0((HiRow+1),i,(HiRow+1));
FOR j := 0 TO HiRow - NewRow DO
X[L2-j] := X[L1-j]
END; (* FOR *)
X[L2 - HiRow + NewRow - 1] := Y[i-1];
IF NewRow > 1 THEN
L1 := LOC0((NewRow-1),i,HiRow);
L2 := LOC0((NewRow-1),i,(HiRow+1));
FOR j := 0 TO NewRow -2 DO
X[L2-j] := X[L1-j]
END; (* FOR *)
END; (* IF *)
END; (* FOR *)è INC(HiRow)
END; (* IF *)
END InsertRow;
(*-------------------------------------------------------------------*)
PROCEDURE ResizeMat(VAR X : ARRAY OF REAL;
OldHiRow, NewHiRow, HiCol: CARDINAL);
(* Procedure to resize matrix X by reassigning the number of rows *)
VAR i, j, L1, L2, Row : CARDINAL;
BEGIN
IF NewHiRow < OldHiRow THEN Row := NewHiRow
ELSE Row := OldHiRow END;
FOR i := HiCol TO 2 BY -1 DO
L1 := LOC0(Row,i,OldHiRow);
L2 := LOC0(Row,i,NewHiRow);
FOR j := 0 TO Row -1 DO
X[L2-j] := X[L1-j]
END; (* FOR *)
END; (* FOR *)
END ResizeMat;
(*-------------------------------------------------------------------*)
PROCEDURE DeleteColumn(VAR X : ARRAY OF REAL; Col, HiRow : CARDINAL;
VAR HiCol : CARDINAL);
(* Procedure to remove column Col from matrix X *)
VAR i, L, MatSize : CARDINAL;
BEGIN
IF Col <= HiCol THEN
IF Col < HiCol THEN
L := LOC0(1,Col,HiRow);
MatSize := HiRow * HiCol;
FOR i := L + HiRow TO MatSize - 1 DO
X[i-HiRow] := X[i]
END; (* FOR *)
END; (* IF *)
DEC(HiCol)
END; (* IF *)
END DeleteColumn;
(*-------------------------------------------------------------------*)
PROCEDURE DeleteRow(VAR X : ARRAY OF REAL; Row, HiCol : CARDINAL;
VAR HiRow : CARDINAL);
(* Procedure to remove row 'Row' from matrix X *)
è
VAR i, j, L1, L2 : CARDINAL;
BEGIN
IF Row <= HiRow THEN
IF Row = HiRow
THEN ResizeMat(X,HiRow,(HiRow-1),HiCol)
ELSE
FOR i := 1 TO HiCol DO
L1 := LOC0(1,i,HiRow);
L2 := LOC0(1,i,(HiRow-1));
IF Row > 1 THEN
FOR j := 0 TO Row-2 DO
X[L2+j] := X[L1+j]
END; (* FOR *)
END; (* IF *)
FOR j := Row-1 TO HiRow-1 DO
X[L2+j] := X[L2+j+1]
END; (* FOR *)
END; (* FOR *)
END; (* IF *)
DEC(HiRow);
END; (* IF *)
END DeleteRow;
END MatMan.